package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.abstraction.IStoreService;
import sg.edu.nus.iss.vmcs.maintenance.*;
import sg.edu.nus.iss.vmcs.machinery.*;
import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.*;

public class MainController
{
	private SimulationController  simulatorCtrl;
	private MachineryController machineryCtrl;
	private MaintenanceController maintenanceCtrl;
	private StoreService storeService;
	private String propertyFile;

	private static MainController instance = null;

	public MainController()
	{
		getInstance();
	}

	private MainController(String blabla, String blablabla){
		
		storeService = (StoreService) ServiceRepository.GetInstance()
		.getService(IStoreService.class);
		simulatorCtrl = new SimulationController(this);
		machineryCtrl = new MachineryController(this);
		
		maintenanceCtrl = new MaintenanceController(this);
	}
	
	public MainController(String propertyFile)
	{
		this.propertyFile = propertyFile;
		getInstance();
	}

	private  MainController getInstance()
	{
		if (instance == null)
		{
			instance = new MainController("nothingButToSurpressStackOverFlowException","temp solution");
			
		}
		return instance;
	}

//	private void initialize()
//	{
//		try
//		{
//			storeService = (StoreService) ServiceRepository.GetInstance()
//					.getService(IStoreService.class);
//			simulatorCtrl = new SimulationController(getInstance());
//			machineryCtrl = new MachineryController(getInstance());
//			machineryCtrl.initialize();
//			maintenanceCtrl = new MaintenanceController(getInstance());
//
//		} catch (VMCSException e)
//		{
//			e.printStackTrace();
//		}
//	}

	public  StoreService getStoreService()
	{
		return getInstance().storeService;
	}

	
	public  MachineryController getMachineryController()
			
	{
		try
		{
			getInstance().machineryCtrl.initialize();
		} catch (VMCSException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return getInstance().machineryCtrl;
	}

	public  MaintenanceController getMaintenanceController()
			
	{
		return getInstance().maintenanceCtrl;
	}

	public SimulationController getSimulatorControlPanel()
	{
		return getInstance().simulatorCtrl;
	}
	
	public  void closeDown()
	{
		try
		{
			getInstance().storeService.closeDown();
		} catch (Exception e)
		{
			System.out.println("Error closing down the stores: " + e);
		}
		getInstance().machineryCtrl.closeDown();
		getInstance().maintenanceCtrl.closeDown();
	}

	// added by Soe ya
	public  void registerObservers(int type, IObserver observer)
			throws VMCSException
	{
		StoreItem item;
		switch (type)
		{
		case (Store.DRINK):
			int drinkSize = getInstance().storeService
					.getStoreSize(Store.DRINK);
			for (int i = 0; i < drinkSize; i++)
			{
				item = getInstance().storeService.getStoreItem(Store.DRINK, i);
				item.attach(observer);
				System.out.println("register cash storeItem:" + i);
			}
			break;
		case (Store.CASH):
			int cashSize = getInstance().storeService.getStoreSize(Store.CASH);
			for (int i = 0; i < cashSize; i++)
			{
				item = getInstance().storeService.getStoreItem(Store.CASH, i);
				item.attach(observer);
				System.out.println("register drinks storeItem:" + i);
			}
			break;
		default:
			break;
		}
	}

	

}
